#include "hal_temper.h"
#include "bsp_adc.h"
#include "bsp_usart.h"

uint8_t NtcIsError[NTC_MAX_NUM];
//tsHalTempCtrl haltempctrl;
const uint16_t WATER_NTC_Tbl_PURE[RT_TABLE_NUM] =     //纯水温度
{
	/* 0   1	2	 	3	  4    5	6	 	7	  8    9  */
	3004, 2968, 2933, 2897, 2860, 2823, 2786, 2748, 2711, 2672, 
	/*10   11	12	 	13   14   15	16	 	17   18   19 */
 	2634, 2595, 2556, 2517, 2478, 2439, 2400, 2360, 2321, 2282, 
	/*20   21	22	 	23   24   25	26	 	27   28   29 */
 	2243, 2203, 2164, 2125, 2087, 2048, 2010, 1972, 1934, 1896, 
	/*30   31	32	 	33   34   35	36	 	37   38   39 */
 	1859, 1822, 1786, 1749, 1713, 1678, 1643, 1609, 1574, 1541, 
	/*40   41	42	 	43   44   45	46	 	47   48   49 */
 	1508, 1475, 1443, 1411, 1380, 1349, 1319, 1289, 1260, 1232, 
	/*50   51	52	 	53   54   55	56		57   58   59 */
 	1204, 1176, 1149, 1122, 1096, 1071, 1046, 1021, 997, 974, 
	/*60   61	62	 	63   64   65	66	 	67   68   69 */
	951,  928,  906,  885,   864, 843,  823,  804,  784, 766, 
	/*70   71	72	 	73   74   75	76	 	77   78   79 */
 	747,  730,  712,  695,   678, 662,  646,  631,  616, 601, 
	/*80   81	82	 	83   84   85	86	 	87   88   89 */
 	587,  573,  559,  546,   533, 520,  508,  496,  484, 473, 
	/*90   91	92	 	93   94   95	96	 	97   98   99 */
 	462,  451,  440,  430,   420, 410,  401,  391,  382, 373, 
	/*100  101  102	 	103  104  105	106	 	107  108  109 */
 	365,  356,  348,  340,   332, 325,  317,  310,  303, 296, 
	/*110  111	112	 	113  114  115	116	 	117  118  119 120 */
	290,  283,  277,  270,   265, 259,  253,  247,  242, 237, 231,
};
const uint16_t WATER_NTC_Tb2[RT_TABLE_NUM] =
{
	/*热罐NTC型号：RFMF503F3950F500A*/
	3858,	3846,	3834,	3822,	3809,	3795,	3781,	3767,	3752,	3737,	//0
	3721,	3704,	3687,	3669,	3651,	3632,	3613,	3593,	3573,	3552,	//10
	3530,	3508,	3485,	3461,	3437,	3413,	3388,	3362,	3335,	3308,	//20
	3281,	3253,	3224,	3195,	3165,	3135,	3104,	3073,	3041,	3009,	//30
	2976,	2943,	2910,	2876,	2842,	2807,	2772,	2737,	2701,	2666,	//40
	2629,	2593,	2557,	2520,	2483,	2447,	2410,	2373,	2336,	2299,	//50
	2262,	2225,	2188,	2151,	2114,	2077,	2041,	2005,	1969,	1933,	//60
	1897,	1862,	1827,	1792,	1757,	1723,	1690,	1656,	1623,	1590,	//70
	1558,	1526,	1494,	1463,	1433,	1402,	1373,	1343,	1314,	1286,	//80
	1258,	1231,	1203,	1177,	1151,	1125,	1100,	1075,	1051,	1027,	//90
	1004,	981,	959,	937,	915,	894,	873,	853,	833,	814,	//100
	795,	777,	758,	741,	723,	706,	690,	674,	658,	643,	//110
	627,																												//120 

	
};
const uint16_t WATER_NTC_Tb3[RT_TABLE_NUM] =
	{
		/*热罐NTC型号：RFMF103F3950F450A*/
		3177, 3099, 	3061, 	3021, 	2981, 	2940, 	2899, 	2857, 	2814, 	2771,	//0
		2727, 2683, 	2639, 	2594, 	2549, 	2504, 	2458, 	2413, 	2367, 	2321, //10
		2275, 2229, 	2184, 	2138, 	2093, 	2048, 	2003, 	1958, 	1914, 	1870, //20
		1826, 1783, 	1741, 	1699, 	1657, 	1616, 	1576, 	1536, 	1497, 	1459, //30
		1421, 1384, 	1347, 	1312, 	1277, 	1242, 	1209, 	1176, 	1143, 	1112, //40
		1081, 1051, 	1022, 	993, 		965, 		938, 		911, 		885, 		860,	 	836,  //50
		812, 	788, 		766, 		744, 		722, 		702, 		681, 		662, 		643,	 	624,  //60
		606, 	588, 		571, 		555, 		539, 		523, 		508, 		494, 		480,	 	466,  //70
		452, 	439, 		427, 		415, 		403, 		391, 		380, 		369, 		359,	 	349,  //80
		339, 	330, 		320, 		311, 		303, 		294, 		286, 		278, 		271,	 	263,  //90
		256, 	249, 		242, 		235, 		229, 		223, 		217, 		211, 		205,	 	200,  //100
		195, 	189, 		184, 		180, 		175, 		170, 		166, 		162, 		157,	 	153,  //110
		149}; 
//-------------------------------------------------------------------------------
// void init_temper(void)
// {
// 	haltempctrl.temp_water = 0;
// 	haltempctrl.temp_heater = 0;
// }

//-------------------------------------------------------------------------------
typedef struct
{
	teADCDMANum ADCNum;     //温度索引 在DMA处处理

}tsNTCCtrl;

const tsNTCCtrl ntcCtrlTable[NTC_MAX_NUM]=
{		//ADC对应的枚举   				//正向引脚               //正向端口号                   /反向引脚             //反向端口号
		{ADC_NTC_HEAT},
		{ADC_NTC_COOL},
		{ADC_NTC_SODA},
		{ADC_NTC_HEAT_L},
		{ADC_NTC_IN_WATER},
};

uint16_t ad_convert_to_temper(uint16_t advalue,teNTCIndex NTCindex)  //@7 查表确定
{
	uint16_t i;
	uint16_t tmp0, tmp1;
	
	uint16_t ntc_table_num;
	const uint16_t *ntc_RT_tbl;
	switch(NTCindex)
	{
		case	NTC_HEAT:	//热罐
		{
			ntc_RT_tbl = WATER_NTC_Tb2; //热罐NTC的表格
			break;
		}
		case	NTC_HEAT_L:	//热罐
		{
			ntc_RT_tbl = WATER_NTC_Tb2; //热罐NTC的表格
			break;
		}
		case 	NTC_COOL:	//冷罐
		{
			ntc_RT_tbl = WATER_NTC_Tb3; //冷罐NTC的表格
			break;
		}
		case	NTC_SODA:	//苏达
		{
			ntc_RT_tbl = WATER_NTC_Tbl_PURE;//纯水NTC表格
			break;
		}
		case	NTC_IN_WATER:	//进水NTC
		{
			ntc_RT_tbl = WATER_NTC_Tb2;//临时先给这个
			break;
		}
		default:ntc_RT_tbl = WATER_NTC_Tbl_PURE; //其他NTC的表格
		break;
	}
//	if(NTCindex == NTC_PURE)
//	{
//			ntc_RT_tbl = WATER_NTC_Tbl_PURE;//纯水NTC表格
//	}
//	else
//	{
//			ntc_RT_tbl = WATER_NTC_Tbl; //其他NTC的表格
//	}
	ntc_table_num = RT_TABLE_NUM;

	if( advalue>= ntc_RT_tbl[0] )
	{
		return NTC_OPEN;
	}
	else if( advalue <= ntc_RT_tbl[ntc_table_num-1] )
	{
		return NTC_SHORT;
		
	}

	
	
	for(i=0; i<ntc_table_num-1; ++i)
	{
		if( (ntc_RT_tbl[i] >= advalue) && (ntc_RT_tbl[i+1] < advalue) )  //@7判断区间
		{
			tmp0 = ntc_RT_tbl[i] - advalue; //3004 -3000
			tmp1 = ntc_RT_tbl[i] - ntc_RT_tbl[i+1];//3004-2968=36
			tmp0 = tmp0*10/tmp1; //@7  在这个里面 占的百分比 然后扩大10倍
			i = i*10 + tmp0; 
			goto EX_TEMPER;
		}
	}

EX_TEMPER:
	return i;
}

//-------------------------------------------------------------------------------
float OnReadNTCtemper(teNTCIndex NTCindex)//@7 缩小10倍的算法
{
	uint16_t data_temper = 0;
    float data_temper_f;
	data_temper = ad_convert_to_temper(adc_read_ch_filter(NTCindex),NTCindex);
	if( (data_temper == NTC_OPEN) || (data_temper == NTC_SHORT) )
	{
		NtcIsError[NTCindex] =1;  //故障
		return data_temper;
		  
	}
  data_temper_f = data_temper/10.0;
  NtcIsError[NTCindex] =0;  //故障解除
  return data_temper_f;

}

uint32_t OnGetNTCError(teNTCIndex NTCindex) //获取NTC故障
{
	return NtcIsError[NTCindex];
}

//-------------------------------------------------------------------------------
// 10 x temper
uint16_t get_ntc_temper_ex(teNTCIndex NTCindex)
{
	uint16_t data_temper = 0;

	data_temper = ad_convert_to_temper(adc_read_ch_filter(NTCindex),NTCindex);

	if( (data_temper == NTC_OPEN) || (data_temper == NTC_SHORT) )
	{
		return data_temper;
	}

//	if( adcindex == ADC_WATER_CUR )
//		haltempctrl.temp_water = data_temper;
//	else if( adcindex == ADC_PIPE_CUR )
//		haltempctrl.temp_heater = data_temper;
    
	return data_temper;
}

//-------------------------------------------------------------------------------
void find_temper_fifoch_min_max(ts_TemperFifoCtrl *pfifo)
{
	int i = 0;

	pfifo->max = pfifo->buf[0];
	pfifo->min = pfifo->buf[0];
	
	for(i = 1; i < TEMPER_FIFO_LEN; i++)
	{
		pfifo->max = MAX(pfifo->max, pfifo->buf[i]);
		pfifo->min = MIN(pfifo->min, pfifo->buf[i]);
	}
}

//-------------------------------------------------------------------------------
// First In First Out
//-------------------------------------------------------------------------------
uint16_t temper_fifoch_push(ts_TemperFifoCtrl *pfifo, uint16_t data)   //@7 推一个与之前的平均
{
	if( pfifo->len >= TEMPER_FIFO_LEN )
	{
		pfifo->sum -= pfifo->buf[pfifo->bottom];
		pfifo->bottom++;
		pfifo->bottom %= TEMPER_FIFO_LEN;// 保证bottom范围在[0,(FIFO_LEN-1)]
		
		pfifo->buf[pfifo->top++] = data;
		pfifo->top %= TEMPER_FIFO_LEN;// 保证top范围在[0,(FIFO_LEN-1)]
		pfifo->sum += data;

		find_temper_fifoch_min_max(pfifo);
		//去掉最大值、最小值     ， +1 四舍五入
		pfifo->average = (pfifo->sum + pfifo->average - pfifo->min - pfifo->max + 1 ) / (TEMPER_FIFO_LEN - 1);
		pfifo->len = TEMPER_FIFO_LEN;
	}
	else
	{
		pfifo->buf[pfifo->top++] = data;
		pfifo->top %= TEMPER_FIFO_LEN;
		pfifo->len++;
		pfifo->sum += data;
		pfifo->average = pfifo->sum / pfifo->len;
	}
	return pfifo->average;
}

//-------------------------------------------------------------------------------
void temper_fifoch_clear(ts_TemperFifoCtrl *pfifo)
{
	pfifo->sum = 0;
	pfifo->average = 0;
	pfifo->len = 0;
	pfifo->top = 0;
	pfifo->bottom = 0;
}



//-------------------------------------------------------------------------------

uint32_t adc_read_ch(teNTCIndex NTCindex)
{
	 return OnReadADCDMAvalue(ntcCtrlTable[NTCindex].ADCNum);
}

//-------------------------------------------------------------------------------
// ???????AD?
uint16_t adc_read_ch_filter(teNTCIndex NTCindex)
{
//	uint32_t sum_val = 0;
	uint32_t ave_val = 0;
//	uint32_t adc_max, adc_min;
//	uint32_t adc_value[ADC_FIL_TIMES] = {0};
//	uint8_t i;
	
	// for(i=0; i<ADC_FIL_TIMES; ++i)//?n?
	// {
	// 	adc_value[i] = adc_read_ch(adcindex);
	// }
   
	// adc_max = adc_value[0];
	// adc_min = adc_value[0];

	// //??
	// for(i=0; i<ADC_FIL_TIMES; ++i)
	// {
	// 	if( adc_value[i] >  adc_max )
	// 	{
	// 		adc_max = adc_value[i];
	// 	}

	// 	if( adc_value[i] <  adc_min )
	// 	{
	// 		adc_min = adc_value[i];
	// 	}
	// 	sum_val += adc_value[i];
	// }
	// printf("%d,%d\n",adc_min,adc_max);
	// ave_val = (sum_val - adc_max - adc_min) / (ADC_FIL_TIMES - 2);
	ave_val = adc_read_ch(NTCindex);
	return ave_val;
}

void OnTemperTest(void)
{
	  print_debug("NTC_HEAT: %f \r\n",OnReadNTCtemper(NTC_HEAT));//热罐温度
		print_debug("NTC_COOL: %f \r\n",OnReadNTCtemper(NTC_COOL));//冷罐NTC
		print_debug("NTC_SODA: %f \r\n",OnReadNTCtemper(NTC_SODA));//苏达NTC

	

}
